package com.model.project;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.TableGenerator;

import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.core.protocols.IKVObject;
import com.core.utils.LocalizableString;
import com.model.KeyValue;

@Entity
public class Version
        implements IKVObject {

	private Integer     id;
	private String      name;
	private String      description;
	private ProjectItem parentProject;



	@Id
	@TableGenerator(allocationSize = 10, table = "Generators", pkColumnName = "generatorName", pkColumnValue = "generatorProjectVersion", name = "generatorProjectVersion")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "generatorProjectVersion")
	public Integer getId () {

		return id;
	}



	public void setId ( Integer id ) {

		this.id = id;
	}



	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@NotFound(action = NotFoundAction.IGNORE)
	public ProjectItem getParentProject () {

		return parentProject;
	}



	public void setParentProject ( ProjectItem parentProject ) {

		this.parentProject = parentProject;
	}



	@Lob
	public String getDescription () {

		return description;
	}



	public void setDescription ( String description ) {

		this.description = description;
	}



	public String getName () {

		return name;
	}



	public void setName ( String name ) {

		this.name = name;
	}



	@Override
	public void assignTo ( IKVObject object ) {

		if ( object == null ) {
			return;
		}

		Version objectToUpdate = (Version) object;
		objectToUpdate.name = this.name;
		objectToUpdate.description = this.description;

	}



	@Override
	public KeyValue idValue () {

		String strIdValue = this.getId().toString();
		return new KeyValue(this.fieldNameAsId(), strIdValue, false);

	}



	@Override
	public String fieldNameAsId () {

		return "id";
	}



	@Override
	public KeyValue nameValue () {

		String name = this.getName();
		String strIdValue = null;
		if ( name != null ) {
			strIdValue = name.toString();
		}
		return new KeyValue(this.fieldNameAsName(), strIdValue, false);

	}



	@Override
	public String fieldNameAsName () {

		return "name";
	}



	public String localizableDescription () {

		return LocalizableString.sharedInstance().localizable("entity.Version.description");
	}
}
